//
//  SISSTextView.m
//  SISSAPP
//
//  Created by 袁应荣 on 13/04/2017.
//  Copyright © 2017 优房网. All rights reserved.
//

#import "SISSTextView.h"

@interface SISSTextView ()

/** 占位文字label */
@property (nonatomic, weak) UILabel *placeholderLabel;

@end
@implementation SISSTextView

- (UILabel *)placeholderLabel {
    
    if (!_placeholderLabel) {
        // 添加一个用来显示占位文字的label
        UILabel *placeholderLabel = [[UILabel alloc] init];
        placeholderLabel.numberOfLines = 0;
        placeholderLabel.textColor = SECONDARY_TEXT_COLOR;
        placeholderLabel.frame =CGRectMake(4, 7, placeholderLabel.frame.size.width, placeholderLabel.frame.size.height);
        [self addSubview:placeholderLabel];
        _placeholderLabel = placeholderLabel;
    }
    return _placeholderLabel;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        // 垂直方向上永远有弹簧效果
        self.alwaysBounceVertical = YES;
        // 滑动收起键盘
        self.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        // 默认字体
        self.font = [UIFont systemFontOfSize:13];
        // 默认的占位文字颜色
        self.placeholderColor = SECONDARY_TEXT_COLOR;
        
        // 监听文字改变
        [[NSNotificationCenter defaultCenter
          ] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame textContainer:(NSTextContainer *)textContainer {
    if (self = [super initWithFrame:frame textContainer:textContainer]) {
        // 垂直方向上永远有弹簧效果
        self.alwaysBounceVertical = YES;
        // 滑动收起键盘
        self.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        // 默认字体
        self.font = [UIFont systemFontOfSize:13];
        // 默认的占位文字颜色
        self.placeholderColor = [UIColor grayColor];
        
        // 监听文字改变
        [[NSNotificationCenter defaultCenter
          ] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}


//- (void)awakeFromNib {
//    [super awakeFromNib];
//    
//    
//}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        // 垂直方向上永远有弹簧效果
        self.alwaysBounceVertical = YES;
        // 滑动收起键盘
        self.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        // 默认字体
        self.font = [UIFont systemFontOfSize:13];
        // 默认的占位文字颜色
        self.placeholderColor = [UIColor grayColor];
        
        // 监听文字改变
        [[NSNotificationCenter defaultCenter
          ] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}


- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/**
 * 监听文字改变
 */
- (void)textDidChange {
    // 只要有文字, 就隐藏占位文字label
    self.placeholderLabel.hidden = self.hasText;
}

/**
 * 更新占位文字的尺寸
 */
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.placeholderLabel.frame =CGRectMake(4, 7, self.frame.size.width - 2 * self.self.placeholderLabel.frame.origin.x, self.placeholderLabel.frame.size.height);
    [self.placeholderLabel sizeToFit];
}

#pragma mark - 重写setter
- (void)setPlaceholderColor:(UIColor *)placeholderColor {
    _placeholderColor = placeholderColor;
    
    self.placeholderLabel.textColor = placeholderColor;
}

- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = [placeholder copy];
    
    self.placeholderLabel.text = placeholder;
    
    [self setNeedsLayout];
}

- (void)setFont:(UIFont *)font {
    [super setFont:font];
    
    self.placeholderLabel.font = [UIFont boldSystemFontOfSize:font.pointSize];
//    self.placeholderLabel.font = font;
    
    [self setNeedsLayout];
}

- (void)setText:(NSString *)text {
    [super setText:text];
    
    [self textDidChange];
}

- (void)setAttributedText:(NSAttributedString *)attributedText {
    [super setAttributedText:attributedText];
    
    [self textDidChange];
}


@end
